<template>
  <div class="home-container" ref="container" @wheel = "handleWheel">
    <ul
      class="carousel-container"
      :style="{
        marginTop,
      }"
      @transitionend = "handleTransitionEnd"
    >
      <li v-for="item in banners" :key="item.id">
        <Carouselitem :carousel = "item"/>
      </li>
    </ul>
    <div @click="switchTo(index - 1)" v-show="index >= 1" class="icon icon-up">
      <Icon type="arrowUp" />
    </div>
    <div
      @click="switchTo(index + 1)"
      v-show="index < banners.length - 1"
      class="icon icon-down"
    >
      <Icon type="arrowDown" />
    </div>
    <ul class="indicator">
      <li
        :class="{
          active: i === index,
        }"
        v-for="(item, i) in banners"
        :key="item.id"
        @click="switchTo(i)"
      ></li>
    </ul>
    <Loading v-if="isLoading"/>
  </div>
</template>

<script>
import { getBanners } from "@/api/banner";
import Carouselitem from "./Carouselitem.vue";
import Icon from "@/components/Icon";
import Loading from "@/components/Loading"

export default {
  data() {
    return {
      banners: [],
      index: 0, //当前显示第几张轮播图
      containerHeight: 0,
      switching: false,
      isLoading: true
    };
  },
  components: {
    Carouselitem,
    Icon,
    Loading,
},

  async created() {
    this.banners = await getBanners();
    this.isLoading = false;
  },
  mounted() {
    this.containerHeight = this.$refs.container.clientHeight;
    window.addEventListener("resize",this.handleResize)
  },
  destroyed(){
    window.removeEventListener("resize",this.handleResize)
  },
  computed: {
    marginTop() {
      return -this.index * this.containerHeight + "px";
    },
  },
  methods: {
    switchTo(i) {
      this.index = i;
    },
    handleWheel(e){
      if(this.switching){
        return;
      }
      if(e.deltaY < 0 && this.index > 0){
        //往上滚动
        this.switching = true;
        this.index --;
      }else if(e.deltaY > 0 && this.index < this.banners.length - 1){
        //往下滚动
        this.switching = true;
        this.index ++ ;
      }
      
    },
    handleTransitionEnd(){
      this.switching = false;
    },
    handleResize(){
      this.containerHeight = this.$refs.container.clientHeight;
    }

  },
};
</script>

<style scoped lang="less">
@import url("~@/style/position.less");
@import url("~@/style/var.less");
.home-container {
  width: 100%;
  height: 100%;
  position: relative;
  overflow: hidden;


  ul {
    margin: 0;
    list-style: none;
    padding: 0;
  }
  .carousel-container {
    width: 100%;
    height: 100%;
    transition: .5s;
    li {
      width: 100%;
      height: 100%;
    }
  }
  .icon {
    @pos: 30px;
    font-size: 20px;
    color: @gray;
    .self-center();
    cursor: pointer;
    transform: translateX(-50%);
    &.icon-up {
      top: @pos;
      animation: jump-up 2s infinite;
    }
    &.icon-down {
      top: auto;
      bottom: @pos;
      animation: jump-down 2s infinite;
    }

    @jump: 5px;
    @keyframes jump-up {
      0% {
        transform: translate(-50%, @jump);
      }
      50% {
        transform: translate(-50%, -@jump);
      }
      100% {
        transform: translate(-50%, @jump);
      }
    }

    @keyframes jump-down {
      0% {
        transform: translate(-50%, -@jump);
      }
      50% {
        transform: translate(-50%, @jump);
      }
      100% {
        transform: translate(-50%, -@jump);
      }
    }
  }
  .indicator {
    .self-center();
    transform: translateY(-50%);
    left: auto;
    right: 20px;
    li {
      width: 7px;
      height: 7px;
      border-radius: 50%;
      background: @words;
      cursor: pointer;
      margin-bottom: 10px;
      border: 1px solid #fff;
      box-sizing: border-box;
      &.active {
        background: #fff;
      }
    }
  }
}
</style>